package com.nofx.mydemo.service.impl;

import com.nofx.mydemo.entity.User;
import com.nofx.mydemo.service.IGitHubLookupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author yanyuchi
 * @date 2021-04-27 20:58
 */
@Slf4j
@Service
public class GitHubLookupServiceImpl implements IGitHubLookupService {

    private final RestTemplate restTemplate;

    public GitHubLookupServiceImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }


    @Async
    @Override
    public CompletableFuture<User> findUser(String user) throws InterruptedException {
        log.info("Looking up "+user);
        String url = String.format("https://api.github.com/users/%s",user);
        User result = restTemplate.getForObject(url,User.class);
        Thread.sleep(1000L);
        return CompletableFuture.completedFuture(result);
    }
}
